package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.itboys.mobile.dto.SundayStoreDto;
import org.itboys.mobile.entity.mongo.SundayStore;
import org.itboys.mobile.service.mongo.web.SundayStoreService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by zhangyang on 2017/12/27.
 * 圣代_实体店_controller_weixin
 */
@Controller
@RequestMapping("/sunday/weixin/store")
public class SundayWeiXinStoreController extends BaseController{

    @Autowired
    private SundayStoreService storeService;

    /**
     * 实体店主页
     * @param request
     * @param response
     * @param
     */
    @RequestMapping(value="/index", method= RequestMethod.GET)
    public String index(HttpServletRequest request,
                        HttpServletResponse response, Model model){
        try{
            SundayMemberSessionDto member = AccountIdHoldUtils.getSessionMember(request);
            return "/aigou/weixin/store/index";
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/store/index----e="+e.getMessage());
            }
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }
    /**
     * 实体店主页
     * @param request
     * @param response
     * @param
     */
    @RequestMapping(value="/detail", method= RequestMethod.GET)
    public String detail(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value = "id", required = true) Long id,
                        Model model){
        try{
            SundayMemberSessionDto member = AccountIdHoldUtils.getSessionMember(request);
            SundayStoreDto store = storeService.getDetail(id);
            model.addAttribute("store",store);
            return "/aigou/weixin/store/detail";
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/store/detail----e="+e.getMessage());
            }
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }
    /**
     * 获取实体店数据
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(value="/select", method= RequestMethod.POST)
    @ApiOperation("【微信端】获取实体店数据")
    public String select(HttpServletRequest request,
                         HttpServletResponse response, Model model){
        try{
            SundayMemberSessionDto member = AccountIdHoldUtils.getSessionMember(request);
            List<SundayStoreDto> list = storeService.select(request);
            model.addAttribute("list",list);
            return "/aigou/weixin/store/data";
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/store/select----e="+e.getMessage());
            }
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }
}
